Type.registerNamespace('ModWatch');

ModWatch.ModAlertListBox = function(element)
{
    ModWatch.ModAlertListBox.initializeBase(this, [element]);
    
    this._modGroup = '';
    this._notifyModMessage = '';
    this._selectedIndices = new Array();
}

ModWatch.ModAlertListBox.prototype = {

	get_modGroup : function()
	{
		return this._modGroup;
	},

	set_modGroup : function(value) {
		if (!value || value == null)
			this._modGroup = '';
		else
			this._modGroup = value;
	},

	get_notifyModMessage : function()
	{
		return this._notifyModMessage;
	},

	set_notifyModMessage : function(value) {
		if (!value || value == null)
			this._notifyModMessage = '';
		else
			this._notifyModMessage = value;
	},

	initialize : function()
	{
        ModWatch.ModAlertListBox.callBaseMethod(this, 'initialize');

		this._saveSelectedIndices();
    },

	checkListChanged : function()
	{
		var cancelChange = false;

		if (ModWatch.ModManagerInstance &&
			ModWatch.ModManagerInstance.isChangeDetected(this.get_modGroup()))
		{
			if (!confirm(this.get_notifyModMessage()))
				cancelChange = true;
		}

		if (cancelChange)
		{
			// roll back the change
			this._revertToSavedIndices();
		}
		else
		{
			// store the new selected index
			this._saveSelectedIndices();
		}
		
		return cancelChange;
	},
	
	_saveSelectedIndices : function()
	{
		var selectedIndexArray = new Array();
		var options = this.get_element().options;

		for (var i = 0; i < options.length; i++)
		{
			if (options[i].selected)
				Array.add(selectedIndexArray, i);
		};

		this._selectedIndices = selectedIndexArray;
	},
	
	_revertToSavedIndices : function()
	{
		var selectedIndexArray = this._selectedIndices;
		var options = this.get_element().options;

		for (var i = 0; i < options.length; i++)
		{
			options[i].selected = Array.contains(selectedIndexArray, i);
		};
	}

}

ModWatch.ModAlertListBox.registerClass('ModWatch.ModAlertListBox', Sys.UI.Control);

if (typeof(Sys) !== 'undefined')
	Sys.Application.notifyScriptLoaded();
